﻿using System.Text.RegularExpressions;
using __Able.Core.Configuration;

namespace __Able.Core.Utilities
{
    /// <summary>
    /// IP限制
    /// </summary>
    public class IpFilter
    {
        /// <summary>
        /// 
        /// </summary>
        private static string allowIps;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool IsAllow(string connectIP)
        {
            if (string.IsNullOrEmpty(allowIps))
                allowIps = ConfigManager.GlobalAppSettings["allowConntectIps"];

            bool ipFlag = false;

            if (string.IsNullOrEmpty(allowIps))
            {
                // 如果是空的话，表示不做任何限制
                ipFlag = true;
            }
            else if (string.IsNullOrEmpty(connectIP))
            {
                ipFlag = false;
            }
            else if (Regex.IsMatch(allowIps, connectIP.Replace(".", "\\.") + @"\s*(,+|$)"))
            {
                ipFlag = true;
            }
            return ipFlag;
        }
    }
}